package com.xtravel.common.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.xtravel.R;

public class DataMoreView extends RelativeLayout implements View.OnClickListener {

    private ProgressBar pbDataMore;
    private TextView tvDataMore;
    private boolean _hasMore;
    private OnClickListener onLoadMoreListener;

    public void hasMore(boolean hasMore) {
        _hasMore = hasMore;
        if (!hasMore) {
            tvDataMore.setText(R.string.str_no_more_data);
        } else {
            tvDataMore.setText(R.string.str_more_data);
        }
    }

    public DataMoreView(Context context, OnClickListener onClickListener) {
        super(context);
        init(context);
        onLoadMoreListener = onClickListener;
    }

    public DataMoreView(Context context, AttributeSet attrs, OnClickListener onClickListener) {
        super(context, attrs);
        init(context);
        onLoadMoreListener = onClickListener;
    }

    public DataMoreView(Context context, AttributeSet attrs, int defStyle, OnClickListener onClickListener) {
        super(context, attrs, defStyle);
        init(context);
        onLoadMoreListener = onClickListener;
    }

    @Override
    public void onClick(View view) {
        if (_hasMore) {
            if (onLoadMoreListener != null) {
                onLoadMoreListener.onClick(view);
        }
        }
    }

    public void onRefreshing(boolean refreshing) {
        if (refreshing) {
            pbDataMore.setVisibility(VISIBLE);
            tvDataMore.setText(R.string.str_more_data_loading);
        } else {
            pbDataMore.setVisibility(GONE);
            tvDataMore.setText(R.string.str_more_data);
        }
    }

    private void init(Context context) {

        RelativeLayout ry = new RelativeLayout(context);
        ViewGroup.LayoutParams ryParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        ry.setLayoutParams(ryParams);


        LinearLayout ly = new LinearLayout(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        ly.setOrientation(LinearLayout.HORIZONTAL);
        ly.setLayoutParams(params);

        pbDataMore = new ProgressBar(context, null, android.R.attr.progressBarStyleSmallInverse);
        LinearLayout.LayoutParams progressParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        progressParams.gravity = Gravity.CENTER_VERTICAL;
        progressParams.setMarginEnd(6);
        pbDataMore.setLayoutParams(progressParams);


        tvDataMore = new TextView(context);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.gravity = Gravity.CENTER_VERTICAL;
        tvDataMore.setLayoutParams(textParams);
        tvDataMore.setGravity(Gravity.CENTER_VERTICAL);
        tvDataMore.setTextColor(Color.parseColor("#A2A2A2"));

        ly.addView(pbDataMore);
        ly.addView(tvDataMore);
        ry.addView(ly);
        this.addView(ry);
        this.setOnClickListener(this);
        hasMore(true);
        //onRefreshing(false);
    }
}
